//
//  RJWarningFirstPicker.m
//  AirQuality
//
//  Created by Po on 15/4/10.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJWarningFirstPicker.h"

@interface RJWarningFirstPicker ()
@property (weak, nonatomic) IBOutlet UIButton *pm25Button;
@property (weak, nonatomic) IBOutlet UIButton *pm10Button;
@property (weak, nonatomic) IBOutlet UIButton *o3Button;
@property (weak, nonatomic) IBOutlet UIButton *so2Button;
@property (weak, nonatomic) IBOutlet UIButton *no2Button;
@property (weak, nonatomic) IBOutlet UIButton *coButton;
@property (weak, nonatomic) IBOutlet UIButton *nothingButton;
@property (weak, nonatomic) IBOutlet UIView *backView;

@property (retain, nonatomic) NSArray * typeArray;
@property (retain, nonatomic) NSArray * data;
@property (retain, nonatomic) NSString * selectString;

- (void)initInterface;
@end

@implementation RJWarningFirstPicker

- (instancetype)initWithFrame:(CGRect)frame data:(NSArray *)data
{
    self = [super initWithFrame:frame];
    if (self) {
        _data = [NSArray arrayWithArray:data];
        [self initInterface];
    }
    return self;
}

- (void)initInterface
{
    NSArray * array = [[NSBundle mainBundle] loadNibNamed:@"RJWarningFirstPicker" owner:self options:nil];
    UIView * view = array[0];
    [view setFrame:ScreenBounds];

    [self addSubview:view];
    
    _typeArray = @[_pm25Button, _pm10Button,_o3Button, _so2Button, _no2Button, _coButton, _nothingButton];
}

- (void)removeSelf
{
    [self removeFromSuperview];
}
#pragma mark - event

/**显示选择结果*/
- (IBAction)pressConfirmButton:(UIButton *)sender {
    if (_delegate) {
        NSMutableArray * stringArray = [[NSMutableArray alloc] init];
        for (UIButton * button in _typeArray) {
            if (button.selected == YES) {
                [stringArray addObject:button.titleLabel.text];
            }
        }
        [_delegate WarningFirstPicker:self selectString:stringArray];
    }
    [self removeSelf];
}

- (IBAction)pressCancelButton:(UIButton *)sender {
    [self removeSelf];
}

- (IBAction)pressButton:(UIButton *)sender {
    sender.selected = !sender.selected;
    
    if ([sender.titleLabel.text isEqualToString:@"  无"] && (sender.selected == YES)) {
        NSUInteger i = 0;
        for (UIButton * button in _typeArray) {
            if (i != _typeArray.count - 1) {
                [button setSelected:NO];
            }
            i ++;
        }
        return;
    }
    
    if (![sender.titleLabel.text isEqualToString:@"  无"] && _nothingButton.selected == YES) {
        [_nothingButton setSelected:NO];
    }
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[touches anyObject] locationInView:self];
    CGRect rect = _backView.frame;
    if (point.x > rect.origin.x && point.x < (rect.origin.x + rect.size.width)
        && point.y < (rect.origin.y + rect.size.height) && point.y > rect.origin.y) {
        return;
    }
    [self removeSelf];
}
@end
